package org.jks.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.model.User;
import org.jks.request.model.UIIds;
import org.jks.request.model.UILogin;
import org.jks.request.model.UIRegister;
import org.jks.response.model.ActionResult;
import org.jks.service.impl.UserServiceImpl;
import org.jks.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource(name = "UserServiceImpl")
	private UserServiceImpl userImpl;

	@RequestMapping("/register")
	public ModelAndView register(UIRegister register, HttpServletRequest request, HttpSession session, HttpServletResponse response,
			Map<String, Object> model) {
		if (register.getPassword1() != null && register.getPassword2() != null
				&& register.getPassword1().trim().length() > 0
				&& register.getPassword1().equals(register.getPassword2())) {
			User user = new User();
			user.setEmail(register.getEmail());
			user.setUsername(register.getUsername());
			user.setPassword(register.getPassword1());
			user.setGender(register.getGender());
			userImpl.save(user);
		}

		Config.init(request, response, session, model, 1);
		return new ModelAndView("login", model);
	}

	@RequestMapping("/login")
	public ModelAndView login(UILogin loginModel,HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model) {
		if (loginModel.getUsername() != null
				&& loginModel.getUsername() != null
				&& loginModel.getUsername().trim().length() > 0) {
			User user = userImpl.findByNameAndPwd(loginModel.getUsername(), loginModel.getPassword());
			if(user != null){
				session.setAttribute("username", user.getUsername());
				if((user.getUsername() != null && user.getUsername().equals("admin"))
						|| (user.getEmail() != null && user.getEmail().equals("admin@admin.com"))){
					return new ModelAndView("redirect:/manage/index");
				}
			}
		}
		
		Config.init(request, response, session, model, 1);
		return new ModelAndView("index", model);
	}

	@RequestMapping("/add")
	public void add(User user, HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model){
		ActionResult ar = new ActionResult();
		try{
			userImpl.save(user);
			ar.setSuccess(true);
			ar.setData(user);
		}
		catch(Exception e){
			
		}
		finally{
			ResponseUtil.returnJson(response, ar);
		}
	}
	
	@RequestMapping("/update")
	public void update(User user, HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model){
		ActionResult ar = new ActionResult();
		try{
			userImpl.update(user);
			ar.setSuccess(true);
			ar.setData(user);
		}
		catch(Exception e){
			
		}
		finally{
			ResponseUtil.returnJson(response, ar);
		}
	}
	
	@RequestMapping("/delete")
	public void delete(UIIds ids, HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model){
	}
	
	@RequestMapping("/check")
	public void check(@RequestParam(value="username") String username, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		ar.setSuccess(true);
		User user = userImpl.findByName(username);
		if(user != null){
			ar.setSuccess(false);
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		List<User> users = userImpl.all();
		model.put("users", users);
		Config.init(request, response, session, model, 0);
		return new ModelAndView("user/index", model);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		ActionResult ar = new ActionResult();
		try{
			List<User> users = userImpl.all();
			ar.setData(users);
			ar.setSuccess(true);
		}
		catch(Exception e){
			
		}
		finally{
			ResponseUtil.returnJson(response, ar);
		}
	}
}
